package com.lyw.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyw.common.utils.R;
import com.lyw.gulimall.ware.feign.ProductFeignService;
import com.lyw.gulimall.ware.vo.SkuHasStockVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyw.common.utils.PageUtils;
import com.lyw.common.utils.Query;

import com.lyw.gulimall.ware.dao.WareSkuDao;
import com.lyw.gulimall.ware.entity.WareSkuEntity;
import com.lyw.gulimall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                new QueryWrapper<WareSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public IPage<Map<String, Object>> queryWareskuPage(Integer page, Integer limit, String skuId, String wareId) {
        Page<WareSkuEntity> result = new Page<>(page, limit);
        LambdaQueryWrapper<WareSkuEntity> lqw = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(skuId)) {
            lqw.eq(WareSkuEntity::getSkuId, skuId);
        }
        if (StringUtils.isNotBlank(wareId)) {
            lqw.eq(WareSkuEntity::getWareId, wareId);
        }
        IPage<Map<String, Object>> pages = baseMapper.selectWareskuPage(result, lqw);
        return pages;
    }

    /**
     *  采购项入库
     * @param skuId 商品id
     * @param wareId 仓库id
     * @param skuNum 商品的数量
     */
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        // 1、如果仓库中没有这个产品就是新增
        List<WareSkuEntity> skuEntities = baseMapper.selectList(new LambdaQueryWrapper<WareSkuEntity>().eq(WareSkuEntity::getSkuId, skuId).eq(WareSkuEntity::getWareId, wareId));
        if (skuEntities != null && skuEntities.size() > 0) {
            // 有的情况
            baseMapper.addStock(skuId, wareId, skuNum);
        } else {
            // 没有的情况
            WareSkuEntity wareSkuEntity = new WareSkuEntity();

            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setWareId(wareId);
            R info = productFeignService.info(skuId);

            try {
                if (info.getCode() == 0) {
                    Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
                    wareSkuEntity.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {

            }

            baseMapper.insert(wareSkuEntity);
        }

    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {

        return baseMapper.selectSkuStock(skuIds);
    }
}